﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Reactive.Disposables;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Text;
using System.Text.Json;
using System.Text.Json.Serialization;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Input;
using System.Windows.Media;
using DynamicData;
using DynamicData.Binding;
using Newtonsoft.Json;
using Serilog;
using Serilog.Core;
using SimpleTool.ActivityUnit.EchoControl;
using SimpleTool.Server;
using Supabase.Postgrest.Models;
using static SimpleTool.ActivityUnit.ActivityDetetor;

namespace SimpleTool.ActivityUnit
{
    public delegate void MessageProPlant(Check Message);

    public enum QuesionSelection
    {
        Accept,
        Ignore
    }

    [Flags]
    public enum MessageProPlantType
    {
        Message = 1,
        Question = 2,
        Success = 4,
        Failed = 8,
        PushFinish = 16,
        HeartBeats = 32,
        MacUpdate = 64,
        Initialize = 128,
        DimProcess = 256
    }

    // 消息通知确认
    public sealed class Check
    {
        public MessageProPlantType Type;
        public object? Message;
        public bool AssertQuestion = false;
        public int Row = 0;

        public Check(MessageProPlantType type, object? message, int row, bool assertQuestion = false)
        {
            Type = type;
            Message = message;
            Row = row;
            AssertQuestion = assertQuestion;
        }
        public Check(MessageProPlantType type, object? message)
        {
            Type = type;
            Message = message;
        }
    }

    public class ActivitySectionModel
    {

        public JsonSerializerOptions JsonOptions = new JsonSerializerOptions()
        {
            DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull,
            IncludeFields = true,
            WriteIndented = true,
        };

        public string cryptKey = "NongJiaBaoLLH";

        public Visibility CanViewChildKeyRegisterVisible = Visibility.Collapsed;

        public ICommand? DBReserveFileAddStdin;

        public ICommand? DBReserveFileSaveStdin;

        public ICommand? DBReserveFileExitStdin;

        public ICommand? RegisterPrdStdin;

        public ICommand? RegisterPrsStdin;

        public ICommand? RegisterAcceptStdin;

        public ICommand? RegisterCancelStdin;

        public ICommand? RegisterChildKeyStdin;

        public ICommand? TestRunStartStdin;

        public ICommand? OpenorConfigArgStdin;

        public ICommand? ConfigSelectionStdin;

        public ICommand? ProductSelectionStdin;

        public string RegisterProductContent = string.Empty;

        public string RegisterProcessContent = string.Empty;

        public string RegisterProductHint = "关联主键";

        public string RegisterProcessHint = "子健名称";

        public string RegisterChildKeyHint = "是否子键";

        public string RunningStartContent = "开始测试";

        public string RunningStartWorkOrder = string.Empty;

        public string RunningStartImei = string.Empty;

        public List<string> CusultColumnNames = [];

        public List<string> ProductSelectedValue = []; //与viewmodel中的集合不同的是，当前集合收集非固定可用选项，viewmodel中的集合仅用于设置默认项

        public Dictionary<string, string> DBWorkOrderMap = [];

        public Dictionary<string, string> ArgReturnReceiptMap = [];

        public int RegisterIsSecondaryKey = 0;

        public object? ControlContent;

        public Action<double, string>? DimProcess;

        public Dictionary<string, string> TableReserveDuty = new()
        {
            {
                "product_config_field", "CREATE TABLE IF NOT EXISTS product_config_field(" +
                                        "field_json TEXT NOT NULL COLLATE NOCASE," +
                                        "field_product TEXT NOT NULL COLLATE NOCASE," +
                                        "field_process PRIMARY KEY TEXT NOT NULL COLLATE NOCASE)"
            },

            {
                "product_config_relation", "CREATE TABLE IF NOT EXISTS product_config_relation(" +
                                           "prd_id TEXT PRIMARY KEY," +
                                           "prd_name TEXT NOT NULL COLLATE NOCASE," +
                                           "prd_process TEXT NOT NULL COLLATE NOCASE," +
                                           "prd_treekey INTEGER NOT NULL CHECK (prd_apply IN (0, 1))," +
                                           "prd_apply INTEGER NOT NULL CHECK (prd_apply IN (0, 1)))"
            },

            {
                "workorder_preview", "CREATE TABLE workorder_preview (" +
                                     "workorder_num TEXT PRIMARY KEY, " +
                                     "product_name TEXT NULL, " +
                                     "product_type TEXT NULL, " +
                                     "sn_start TEXT NULL, " +
                                     "sn_end TEXT NULL, " +
                                     "imei_start TEXT NULL, " +
                                     "imei_end TEXT NULL, " +
                                     "version TEXT NULL)"
            },

            {
                "workorder_unfold", "CREATE TABLE workorder_unfold (" +
                                    "id TEXT PRIMARY KEY, " +
                                    "sn TEXT, " +
                                    "sn_suffix TEXT, " +
                                    "imei TEXT, " +
                                    "imei_suffix TEXT, " +
                                    "secret_key TEXT, " +
                                    "onenet_product_id TEXT, " +
                                    "onenet_register_id TEXT, " +
                                    "product_api_key TEXT, " +
                                    "device_api_key TEXT, " +
                                    "onenet_verify_info TEXT, " +
                                    "blue_mac TEXT, " +
                                    "state TEXT, " +
                                    "product_name TEXT, " +
                                    "product_type TEXT, " +
                                    "product_num TEXT, " +
                                    "product_version TEXT, " +
                                    "workorder_num TEXT, " +
                                    "ng TEXT, " +
                                    "ng1 TEXT, " +
                                    "import_time TEXT, " +
                                    "takes_time TEXT)" 
            },

            {
                "dbfield_relation", "CREATE TABLE IF NOT EXISTS dbfield_relation(" +
                                    "prd_name TEXT NOT NULL COLLATE NOCASE," +
                                    "field_json TEXT NOT NULL COLLATE NOCASE," +
                                    "consult_columns TEXT NOT NULL COLLATE NOCASE)"
            }
        };

        public sealed class MultilAtrributeStruct
        {
            public bool Status = true;

            public int Priority = 0;

            public string Step;

            public string Command;

            public string PassFlag;

            public string ErrorFlag;

            public string? PassMatch;

            public string? PassValueVirify;

            public int? SendDelayMs;

            public int? TimeoutMs;

            public int? MaxRetryCount;

            public int? MaxContentLength;

            public int? RetryDelayMs;

            public bool? IsLinkedToPrevious;

            public bool? IsCombination;

            public bool? IsBluetoothBoard;

            public bool? IsHandDownValue;

            public string? BluethAccording;

            public string? AddtionAccording;

            public bool? IsaddtionXOR;

            public string? ValidValueRange;

            public string? ValueProcessingType;

            public string? DbFieldMapping;

            public string? AppendType;

            public string? AppendValue;

            public string? ContainAtthis;

            public MultilAtrributeStruct(string step, string command, string passFlag, string errorFlag)
            {
                Step = step;
                Command = command;
                PassFlag = passFlag;
                ErrorFlag = errorFlag;
            }

            public MultilAtrributeStruct(string step)
            {
                Step = step;
                Command = string.Empty;
                PassFlag = string.Empty;
                ErrorFlag = string.Empty;
            }

            public MultilAtrributeStruct()
            {
                Step = string.Empty;
                Command = string.Empty;
                PassFlag = string.Empty;
                ErrorFlag = string.Empty;
            }
        }

        public sealed class WorkOrderMapInfoStruct
        {
            // 主键ID
            public string? Id { get; set; }

            // 序列号
            public string? Sn { get; set; }

            // IMEI码
            public string? Imei { get; set; }

            // Secret密钥
            public string? SecretKey { get; set; }

            // OneNet产品ID
            public string? OnenetProductId { get; set; }

            // OneNet注册ID
            public string? OnenetRegisterId { get; set; }

            // 产品API密钥
            public string? ProductApiKey { get; set; }

            // 设备API密钥
            public string? DeviceApiKey { get; set; }

            // OneNet验证信息
            public string? OnenetVerifyInfo { get; set; }

            // 蓝牙MAC地址
            public string? BlueMac { get; set; }

            // 状态
            public string? State { get; set; }

            // 产品名称
            public string? ProductName { get; set; }

            // 产品类型
            public string? ProductType { get; set; }

            // 产品编号
            public string? ProductNum { get; set; }

            // 产品版本
            public string? ProductVersion { get; set; }

            // 工单编号
            public string? WorkorderNum { get; set; }

            // SIM卡号
            public string? Sim { get; set; }

            // 版本号
            public string? Version { get; set; }

            public WorkOrderMapInfoStruct() { }

        }

        public sealed class ActivityAssistBehivor
        {
            public static readonly ActivityAssistBehivor Behivor = new();

            public readonly List<string> fieldHideds = [];

            public readonly List<string> LongTimeTak = [];

            public readonly Dictionary<string, bool> fieldIsPrimaryMap = [];

            public readonly List<string> fieldCanCheckNames = [];

            public readonly Dictionary<string, KeyValuePair<string, DependencyDBPropertyType>> DBDependencyMap = [];

            public readonly Dictionary<string, string> fieldReserveDutyMap = [];

            public readonly Dictionary<string, (string, int)> ParamReserveDutyMap = [];

            public readonly Dictionary<string, string> fieldTranslationMap = [];

            public readonly Dictionary<string, List<string>> fieldExepctPushMap = [];

            public readonly Dictionary<string, (string, int)> TestFlagsMap = [];

            public readonly List<string> ProductFixedOpptions = [];

            private ActivityAssistBehivor()
            {
                //测试标识
                TestFlagsMap.Add("HYC-00", ("烧录初始状态", 0));
                TestFlagsMap.Add("HYC-81", ("整机测试1", 1));
                TestFlagsMap.Add("HYC-03", ("老化测试", 2));
                TestFlagsMap.Add("HYC-83", ("老化测试", 3));
                TestFlagsMap.Add("HYC-93", ("功率检测", 4));
                TestFlagsMap.Add("HYC-97", ("写号校号", 5));
                TestFlagsMap.Add("HYC-9f", ("整机测试2", 6));

                // 显示的默认可配置项
                fieldIsPrimaryMap.Add("Status", true);
                fieldIsPrimaryMap.Add("Priority", true);
                fieldIsPrimaryMap.Add("Step", true);
                fieldIsPrimaryMap.Add("Command", true);
                fieldIsPrimaryMap.Add("PassFlag", true);
                fieldIsPrimaryMap.Add("ErrorFlag", true);
                fieldIsPrimaryMap.Add("PassMatch", false);
                fieldIsPrimaryMap.Add("PassValueVirify", false);
                fieldIsPrimaryMap.Add("TimeoutMs", false);
                fieldIsPrimaryMap.Add("SendDelayMs", false);
                fieldIsPrimaryMap.Add("MaxRetryCount", false);
                fieldIsPrimaryMap.Add("MaxContentLength", false);
                fieldIsPrimaryMap.Add("RetryDelayMs", false);
                fieldIsPrimaryMap.Add("IsLinkedToPrevious", false);
                fieldIsPrimaryMap.Add("ValidValueRange", false);
                fieldIsPrimaryMap.Add("ValueProcessingType", false);
                fieldIsPrimaryMap.Add("DbFieldMapping", false);
                fieldIsPrimaryMap.Add("AppendType", false);
                fieldIsPrimaryMap.Add("AppendValue", false);
                fieldIsPrimaryMap.Add("IsCombination", false); 
                fieldIsPrimaryMap.Add("IsaddtionXOR", false);
                fieldIsPrimaryMap.Add("IsBluetoothBoard", false);
                fieldIsPrimaryMap.Add("BluethAccording", false);
                fieldIsPrimaryMap.Add("AddtionAccording", false);
                fieldIsPrimaryMap.Add("ContainAtthis", false);
                fieldIsPrimaryMap.Add("IsHandDownValue", false);

                //匹配项
                fieldCanCheckNames.Add("PassFlag");
                fieldCanCheckNames.Add("PassMatch");
                fieldCanCheckNames.Add("PassValueVirify");
                fieldCanCheckNames.Add("MaxContentLength");
                fieldCanCheckNames.Add("IsLinkedToPrevious");
                fieldCanCheckNames.Add("ValidValueRange");
                fieldCanCheckNames.Add("AppendValue");
                fieldCanCheckNames.Add("AppendValue"); 
                fieldCanCheckNames.Add("ContainAtthis");
                fieldCanCheckNames.Add("WriteDBFieldName");

                //不可跳过等待项
                LongTimeTak.Add("prod AT+TEST=NFC_TEST");

                //固定产品项
                ProductFixedOpptions.Add("产品管理");
                ProductFixedOpptions.Add("预置数据库");

                //数据库映射
                fieldReserveDutyMap.Add("Id", "主键ID");
                fieldReserveDutyMap.Add("Sn", "序列号");
                fieldReserveDutyMap.Add("Imei", "IMEI码");
                fieldReserveDutyMap.Add("SecretKey", "Secret密钥");
                fieldReserveDutyMap.Add("OnenetProductId", "OneNet产品ID");
                fieldReserveDutyMap.Add("OnenetRegisterId", "OneNet注册ID");
                fieldReserveDutyMap.Add("ProductApiKey", "产品API密钥");
                fieldReserveDutyMap.Add("DeviceApiKey", "设备API密钥");
                fieldReserveDutyMap.Add("OnenetVerifyInfo", "OneNet验证信息");
                fieldReserveDutyMap.Add("BlueMac", "蓝牙MAC地址");
                fieldReserveDutyMap.Add("State", "状态");
                fieldReserveDutyMap.Add("ProductName", "产品名称");
                fieldReserveDutyMap.Add("ProductType", "产品类型");
                fieldReserveDutyMap.Add("ProductNum", "产品编号");
                fieldReserveDutyMap.Add("ProductVersion", "产品版本");
                fieldReserveDutyMap.Add("WorkorderNum", "工单编号");
                fieldReserveDutyMap.Add("Sim", "SIM卡号");
                fieldReserveDutyMap.Add("Version", "版本号");

                //数据库字段依赖项
                DBDependencyMap.Add("SnSuffix", new KeyValuePair<string, DependencyDBPropertyType>("Sn", DependencyDBPropertyType.difference));
                DBDependencyMap.Add("SnStart", new KeyValuePair<string, DependencyDBPropertyType>("Sn", DependencyDBPropertyType.start));
                DBDependencyMap.Add("SnEnd", new KeyValuePair<string, DependencyDBPropertyType>("Sn", DependencyDBPropertyType.end));
                DBDependencyMap.Add("ImeiSuffix", new KeyValuePair<string, DependencyDBPropertyType>("Imei", DependencyDBPropertyType.difference));
                DBDependencyMap.Add("ImeiStart", new KeyValuePair<string, DependencyDBPropertyType>("Imei", DependencyDBPropertyType.start));
                DBDependencyMap.Add("ImeiEnd", new KeyValuePair<string, DependencyDBPropertyType>("Imei", DependencyDBPropertyType.end));

                //配置项中文映射
                fieldTranslationMap.Add("Status", "状态");
                fieldTranslationMap.Add("Priority", "优先级");
                fieldTranslationMap.Add("Step", "步骤");
                fieldTranslationMap.Add("Command", "指令");
                fieldTranslationMap.Add("PassFlag", "通过标识");
                fieldTranslationMap.Add("ErrorFlag", "异常标识");
                fieldTranslationMap.Add("PassMatch", "通过值匹配");
                fieldTranslationMap.Add("PassValueVirify", "通过值校验");
                fieldTranslationMap.Add("SendDelayMs", "延迟(ms)");
                fieldTranslationMap.Add("MaxRetryCount", "重试次数");
                fieldTranslationMap.Add("MaxContentLength", "字符长度校验");
                fieldTranslationMap.Add("TimeoutMs", "测试项超时(ms)");
                fieldTranslationMap.Add("RetryDelayMs", "重试延迟(ms)");
                fieldTranslationMap.Add("IsLinkedToPrevious", "关联上个输出");
                fieldTranslationMap.Add("IsHandDownValue", "传承值");
                fieldTranslationMap.Add("ValidValueRange", "数据有效区间");
                fieldTranslationMap.Add("ValueProcessingType", "数据处理类型");
                fieldTranslationMap.Add("WriteDBFieldName", "数据库字段(更新值)");
                fieldTranslationMap.Add("AppendType", "追加类型"); 
                fieldTranslationMap.Add("AppendValue", "追加数据");
                fieldTranslationMap.Add("IsCombination", "拼接指令");
                fieldTranslationMap.Add("BluethAccording", "蓝牙地址中间字符"); 
                fieldTranslationMap.Add("AddtionAccording", "拼接中间字符");
                fieldTranslationMap.Add("IsBluetoothBoard", "连接蓝牙板串口");
                fieldTranslationMap.Add("IsaddtionXOR", "位或校验");
                fieldTranslationMap.Add("ContainAtthis", "包含");

                //参数映射
                ParamReserveDutyMap.Add("PCDCODE", ("PCD条码", 1));
                ParamReserveDutyMap.Add("IMEI", ("IMEI", 2));
                ParamReserveDutyMap.Add("SN", ("SN", 3));
                ParamReserveDutyMap.Add("BLUE", ("蓝牙", 4));
                ParamReserveDutyMap.Add("DOID", ("DOID", 5));
                ParamReserveDutyMap.Add("QRCODE_1", ("二维码1", 6));
                ParamReserveDutyMap.Add("QRCODE_2", ("二维码2", 7));

                fieldExepctPushMap.Add("Status", ["False", "True"]);
                fieldExepctPushMap.Add("Priority", [.. Enumerable.Range(1, 25).Select(x=>x.GetSafetyChar())]);
                fieldExepctPushMap.Add("Step", [
                    "产测模式",
                    "读取信号强度",
                    "喇叭测试",
                    "网络指示灯测试",
                    "充电指示灯测试",
                    "触摸按键测试",
                    "sim卡测试",
                    "读取测试标志",
                    "软件版本测试",
                    "设备型号设置",
                    "防拆按键测试",
                    "漏电检测测试",
                    "温度传感器测试",
                    "功率测试参数写入",
                    "蓝牙设置测试",
                    "蓝牙获取测试",
                    "蓝牙扫描测试",
                    "第一次刷卡测试",
                    "第二次刷卡测试",
                    "刷卡测试",
                    "退出产测"]);
                fieldExepctPushMap.Add("Command", [
                    "prod AT+TEST=TEST_MODE",
                    "prod AT+TEST=EXIT_TEST_MODE",
                    "prod AT+TEST=SW_VER",
                    "prod AT+TEST=SET_FLAG",
                    "prod AT+TEST=SET_MAC",
                    "prod AT+TEST=SET_IMEI",
                    "prod AT+TEST=SET_SN",
                    "prod AT+TEST=GET_IMEI",
                    "prod AT+TEST=GET_MAC",
                    "prod AT+TEST=OUTLET_LED",
                    "prod AT+TEST=SIM_TEST",
                    "prod AT+TEST=READ_FLAG",
                    "prod AT+TEST=LEAK_TEST",
                    "prod AT+TEST=VOICE_TEST",
                    "prod AT+TEST=TEMP_VALUE",
                    "prod AT+TEST=NET_LED",
                    "prod AT+TEST=NFC_TEST",
                    "prod AT+TEST=CSQ",
                    "prod AT+TEST=SET_PW_PARAMS,5,21,43",
                    "prod AT+TEST=TOUCH_TEST",
                    "AT+SCANADD=",
                    "AT+TEST=SET_MQTT_PRODUCT_ID",
                    "AT+TEST=READ_MQTT_PRODUCT_ID",
                    "AT+TEST=SET_MQTT_ACCESS_KEY",
                    "AT+TEST=READ_MQTT_ACCESS_KEY"]);
                fieldExepctPushMap.Add("PassFlag", [
                    "OK",
                    "SCAN_OK",
                    "Card_ID",
                    "FLAG",
                    "READ IMSI",
                    "READ CSQ",
                    "HYC-C"]);
                fieldExepctPushMap.Add("ErrorFlag", ["NG", "SCAN_TIME_OUT", "SCAN_NG"]);
                fieldExepctPushMap.Add("PassMatch", [
                    "(?-i)Card_ID\\W*((:?[^\\W]+\\s+){3}[^\\W]{2})",
                    "(?-i)FLAG\\W*([^\\W]+)",
                    "(?-i)TEMP\\W*([\\d]+)",
                    "(?-i)(([0-9]+[\\.*])+[0-9]*)",
                    "(?-i)CSQ\\W*(([^\\W]+[^\\s]*)+)",
                    "(?-i)IMSI\\W*(([^\\W]+[^\\s]*)+)",
                    "(?-i)UID\\s*=\\s*\\W*([0-9a-zA-Z]+)\\W*"]);
                fieldExepctPushMap.Add("SendDelayMs", [.. Enumerable.Range(1, 25).Select(x => x.GetSafetyChar() + "000")]);
                fieldExepctPushMap.Add("MaxRetryCount", [.. Enumerable.Range(1, 10).Select(x => x.GetSafetyChar())]);
                fieldExepctPushMap.Add("MaxContentLength", [.. Enumerable.Range(1, 50).Select(x => x.GetSafetyChar())]);
                fieldExepctPushMap.Add("TimeoutMs", [.. Enumerable.Range(1, 25).Select(x => x.GetSafetyChar() + "000")]);
                fieldExepctPushMap.Add("RetryDelayMs", [.. Enumerable.Range(1, 25).Select(x => x.GetSafetyChar() + "000")]);
                fieldExepctPushMap.Add("IsLinkedToPrevious", ["False", "True"]); 
                fieldExepctPushMap.Add("IsCombination", ["False", "True"]);
                fieldExepctPushMap.Add("BluethAccording", ["-", ":"]);
                fieldExepctPushMap.Add("AddtionAccording", [",", ":"]);
                fieldExepctPushMap.Add("IsaddtionXOR", ["False", "True"]);
                fieldExepctPushMap.Add("IsBluetoothBoard", ["False", "True"]);
                fieldExepctPushMap.Add("IsHandDownValue", ["False", "True"]);
                fieldExepctPushMap.Add("ContainAtthis", ["IMEI", "SN", "QRCODE", "DOID"]);
                fieldExepctPushMap.Add("WriteDBFieldName", ["写", "读"]);
                fieldExepctPushMap.Add("ValidValueRange", ["6, 99", "10, 50", "0,60"]);
                fieldExepctPushMap.Add("ValueProcessingType", ["左值", "右值", "整值", "蓝牙"]);
                fieldExepctPushMap.Add("AppendType", ["蓝牙", "序列号", "IMEI号", "产品ID", "产品密钥"]);
                fieldExepctPushMap.Add("AppendValue", ["实时蓝牙扫描", "数据库", "随机(蓝牙)", "r6975tQ0t1", "i+Op9YvTx/+xpQ1fh991C3+BBQ+PGz0laZ9rxes7O9c="]);
            }
        }

        public sealed class ActivityListBoxItemStruct
        {
            public string ActivityName { set; get; } = string.Empty;

            public Brush ItemBack { set; get; } = Brushes.Transparent;

            public ActivityListBoxItemStruct(string activityName, string itemBack)
            {
                ActivityName = activityName;
                object? brushObject = new BrushConverter().ConvertFrom(itemBack);
                if (brushObject != null) ItemBack = (Brush)brushObject;
            }
            public ActivityListBoxItemStruct(string activityName, Brush itemBack)
            {
                ActivityName = activityName;
                if (itemBack != null) ItemBack = itemBack;
            }

            public ActivityListBoxItemStruct(string activityName, Color itemBack)
            {
                ActivityName = activityName;
                ItemBack = new SolidColorBrush(itemBack);
            }
        }

        public sealed class SelectorContentStruct
        {
            public string Icon { set; get; }

            public string Name { set; get; }

            public Brush ForeGround { set; get; }

            public Visibility Visibility { set; get; } = Visibility.Collapsed;

            public SelectorContentStruct(string icon, string name, Brush? foreGround = null)
            {
                this.Icon = icon;
                this.Name = name;
                if (foreGround is null)
                {
                    ForeGround = "DeepMatrix".FindSolidBrush();
                }
                else
                {
                    ForeGround = foreGround;
                }
            }
        }

    }

    [Supabase.Postgrest.Attributes.Table("product_config_field")]
    public sealed class ProductConfigField : BaseModel
    {

        [Supabase.Postgrest.Attributes.Column("code")]
        public string Code { set; get; } = string.Empty;

        [Supabase.Postgrest.Attributes.Column("field_json")]
        public string FieldJson { set; get; } = string.Empty;

        [Supabase.Postgrest.Attributes.Column("field_product")]
        public string FieldProduct { set; get; } = string.Empty;

        [Supabase.Postgrest.Attributes.Column("field_process")]
        public string FieldProcess { set; get; } = string.Empty;

        [Supabase.Postgrest.Attributes.Column("alter_time")]
        public string AlterTime { set; get; } = DateTime.Now.ToString();
    }

    [Supabase.Postgrest.Attributes.Table("product_config_relation")]
    public sealed class ProductConfigRelation : BaseModel
    {
        [Supabase.Postgrest.Attributes.Column("prd_id"), Supabase.Postgrest.Attributes.PrimaryKey]
        public string PrdId { set; get; } = string.Empty;

        [Supabase.Postgrest.Attributes.Column("prd_name")]
        public string PrdName { set; get; } = string.Empty;

        [Supabase.Postgrest.Attributes.Column("prd_process")]
        public string PrdProcess { set; get; } = string.Empty;

        [Supabase.Postgrest.Attributes.Column("prd_treekey")]
        public int PrdTreeKey { set; get; } = 0;

        [Supabase.Postgrest.Attributes.Column("prd_apply")]
        public int PrdApply { set; get; } = 0;
    }

    [Supabase.Postgrest.Attributes.Table("workorder_unfold")]
    public sealed class WorkOrderUnfold : BaseModel
    {
        [Supabase.Postgrest.Attributes.Column("id"), Supabase.Postgrest.Attributes.PrimaryKey]
        public string Id { get; set; } = string.Empty;

        [Supabase.Postgrest.Attributes.Column("sn")]
        public string Sn { get; set; } = string.Empty;

        [Supabase.Postgrest.Attributes.Column("sn_suffix")]
        public string SnSuffix { get; set; } = string.Empty;

        [Supabase.Postgrest.Attributes.Column("imei")]
        public string Imei { get; set; } = string.Empty;

        [Supabase.Postgrest.Attributes.Column("imei_suffix")]
        public string ImeiSuffix { get; set; } = string.Empty;

        [Supabase.Postgrest.Attributes.Column("secret_key")]
        public string SecretKey { get; set; } = string.Empty;

        [Supabase.Postgrest.Attributes.Column("onenet_product_id")]
        public string OnenetProductId { get; set; } = string.Empty;

        [Supabase.Postgrest.Attributes.Column("onenet_register_id")]
        public string OnenetRegisterId { get; set; } = string.Empty;

        [Supabase.Postgrest.Attributes.Column("product_api_key")]
        public string ProductApiKey { get; set; } = string.Empty;

        [Supabase.Postgrest.Attributes.Column("device_api_key")]
        public string DeviceApiKey { get; set; } = string.Empty;

        [Supabase.Postgrest.Attributes.Column("onenet_verify_info")]
        public string OnenetVerifyInfo { get; set; } = string.Empty;

        [Supabase.Postgrest.Attributes.Column("blue_mac")]
        public string BlueMac { get; set; } = string.Empty;

        [Supabase.Postgrest.Attributes.Column("state")]
        public string State { get; set; } = string.Empty;

        [Supabase.Postgrest.Attributes.Column("product_name")]
        public string ProductName { get; set; } = string.Empty;

        [Supabase.Postgrest.Attributes.Column("product_type")]
        public string ProductType { get; set; } = string.Empty;

        [Supabase.Postgrest.Attributes.Column("product_num")]
        public string ProductNum { get; set; } = string.Empty;

        [Supabase.Postgrest.Attributes.Column("product_version")]
        public string ProductVersion { get; set; } = string.Empty;

        [Supabase.Postgrest.Attributes.Column("workorder_num")]
        public string WorkorderNum { get; set; } = string.Empty;

        [Supabase.Postgrest.Attributes.Column("ng")]
        public string Ng { get; set; } = string.Empty;

        [Supabase.Postgrest.Attributes.Column("ng1")]
        public string Ng1 { get; set; } = string.Empty;

        [Supabase.Postgrest.Attributes.Column("import_time")]
        public string ImportTime { get; set; } = string.Empty;

        [Supabase.Postgrest.Attributes.Column("takes_time")]
        public string TakesTime { get; set; } = string.Empty;

    }

    [Supabase.Postgrest.Attributes.Table("workorder_preview")]
    public sealed class WorkOrderPreview : BaseModel
    {
        [Supabase.Postgrest.Attributes.Column("workorder_num"), Supabase.Postgrest.Attributes.PrimaryKey]
        public string WorkorderNum { get; set; } = string.Empty;

        [Supabase.Postgrest.Attributes.Column("product_name")]
        public string ProductName { get; set; } = string.Empty;

        [Supabase.Postgrest.Attributes.Column("product_type")]
        public string ProductType { get; set; } = string.Empty;

        [Supabase.Postgrest.Attributes.Column("sn_start")]
        public string SnStart { get; set; } = string.Empty;

        [Supabase.Postgrest.Attributes.Column("sn_end")]
        public string SnEnd { get; set; } = string.Empty;

        [Supabase.Postgrest.Attributes.Column("imei_start")]
        public string ImeiStart { get; set; } = string.Empty;

        [Supabase.Postgrest.Attributes.Column("imei_end")]
        public string ImeiEnd { get; set; } = string.Empty;

        [Supabase.Postgrest.Attributes.Column("version")]
        public string Version { get; set; } = string.Empty;
    }

    [Supabase.Postgrest.Attributes.Table("dbfield_relation")]
    public sealed class DbFieldRelation : BaseModel
    {
        [Supabase.Postgrest.Attributes.Column("prd_name")]
        public string ProductName { get; set; } = string.Empty;

        [Supabase.Postgrest.Attributes.Column("field_json"), Supabase.Postgrest.Attributes.PrimaryKey]
        public string FieldJson { get; set; } = string.Empty;

        [Supabase.Postgrest.Attributes.Column("consult_columns")]
        public string ConsultColumns { get; set; } = string.Empty;
    }
}
